#include "key.h"

Key_t keys[4]={0};
uint8_t keyDown,keyMode;
void Key_Scan(void)
{
    keys[0].pin=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
    keys[1].pin=HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4);
    keys[2].pin=HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3);
    keys[3].pin=HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2);
    for(uint8_t i=0;i<4;i++)
    {
        switch(keys[i].state)
        {
            case 0:
                if((keys[i].pin==0&&i!=0)||(i==0&&keys[i].pin==1))
                {
                    keys[i].state=1;
                    keys[i].debounce=0;
                }
                break;
            case 1:
                if((keys[i].pin==0&&i!=0)||(i==0&&keys[i].pin==1))
                {
                    keys[i].debounce++;
                }
                else
                {
                    if(keys[i].debounce>99)
                    {
                        keyDown=i+1;
                        keyMode=2;
                    }
                    else if(keys[i].debounce>0)
                    {
                        keyDown=i+1;
                        keyMode=1;
                    }
                    keys[i].debounce=0;
                    keys[i].state=0;
                }
                break;
            default:
                break;
        }
        
    }
}

void Key_Reset(void)
{
    keyDown=keyMode=0;
}


